package com.aaa.service.impl;


import com.aaa.dao.RegisteredItemDao;
import com.aaa.entity.CheckItem;
import com.aaa.entity.RegisteredItem;
import com.aaa.service.RegisteredItemService;
import com.aaa.util.AccountName;
import com.aaa.vo.RegisteredItemVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.aaa.service.impl
 * @Description:
 * @CreateTime: 2023-03-27  09:04
 * @Author: ql
 */
@Service
public class RegisteredItemServiceImpl implements RegisteredItemService {


    @Autowired
    private RegisteredItemDao registeredItemDao;
    @Override
    public Result<List<RegisteredItem>> queryByRegItemName(String regItemName) {

        QueryWrapper<RegisteredItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
//        queryWrapper.eq("reg_item_name",regItemName);
        List<RegisteredItem> registeredItems = registeredItemDao.selectList(queryWrapper);


        return new Result<>(2000,"ok",registeredItems);
    }

    @Override
    public Result<IPage<RegisteredItem>> queryByPage(RegisteredItemVo vo) {


        System.out.println("+++++++++++ = " + vo);






        Page<RegisteredItem> registeredItemPage = new Page<>(vo.getPageNum(), vo.getPageSize());
        QueryWrapper<RegisteredItem> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(vo.getStatus())){
            queryWrapper.like("status",vo.getStatus());
        }if (StringUtils.hasText(vo.getRegItemName())){
            queryWrapper.like("reg_item_name",vo.getRegItemName());
        }
        IPage<RegisteredItem> registeredItemPage1 = registeredItemDao.selectPage(registeredItemPage, queryWrapper);

        return new Result<>(2000,"ok",registeredItemPage1);
    }

    @Override
    public Result<RegisteredItem> queryById(Long id) {
        QueryWrapper<RegisteredItem> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("reg_item_id",id);
        RegisteredItem registeredItem = registeredItemDao.selectOne(wrapper);
        return new Result<>(2000,"ok",registeredItem);
    }

    @Autowired
    private AccountName accountName;
    @Override
    public Result<String> addOrUpdRegisteredItem(RegisteredItem registeredItem) {
        String userName = accountName.getAccountName();
        System.out.println("添加或修改的====================== " + registeredItem);
        int num;
        if (registeredItem.getRegItemId()!=null){
            registeredItem.setUpdateBy(userName);
            registeredItem.setUpdateTime(new Date());
            //修改
            num = registeredItemDao.updateById(registeredItem);
        }else {
            registeredItem.setCreateBy(userName);
            registeredItem.setCreateTime(new Date());
            num = registeredItemDao.insert(registeredItem);
        }
        if (num>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result<String> deleteByIds(Long[] id) {
        List<Long> collect1 = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect1);
        int i = registeredItemDao.deleteBatchIds(collect1);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }
}
